package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Set;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BClassification;
import com.ruoyi.system.domain.BThing;
import com.ruoyi.system.domain.Job;
import com.ruoyi.system.domain.PPosition;
import com.ruoyi.system.service.*;
import com.ruoyi.web.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;

import javax.annotation.Resource;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Resource
    private IBThingService ibThingService;

    @Resource
    private IPPositionService positionService;

    @Resource
    private IJobService jobService;

    @Resource
    private IBClassificationService ibClassificationService;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @GetMapping("/jobsnewType")
    public AjaxResult getJobsType(){
        //从官网同步到小程序
        //1.复制分类
        BClassification bClassification = new BClassification();
        bClassification.setSyncStatus(0L);
        List<BClassification> bClassificationList =
                ibClassificationService.selectBClassificationList(bClassification);
        if (bClassificationList != null && bClassificationList.size() > 0) {
            for (int i = 0; i < bClassificationList.size(); i++) {
                //是否已经存在
                PPosition pPosition = new PPosition();
                pPosition.setName(bClassificationList.get(i).getTitle().trim());
                List<PPosition> positionList = positionService.selectPPositionList(pPosition);
                if (positionList != null && positionList.size() > 0) {

                } else {
                    //不存在，则新增
                    pPosition.setEnableStatus(0);
                    String id = new UuidUtils().getShortUuid();
                    pPosition.setId(id);
                    positionService.insertPPosition(pPosition);
                    PPosition pPosition2 = new PPosition();
                    pPosition2.setName(pPosition.getName());
                    String id2 = new UuidUtils().getShortUuid();
                    pPosition2.setId(id2);
                    pPosition2.setEnableStatus(0);
                    pPosition2.setpId(id);
                    positionService.insertPPosition(pPosition2);
                    BClassification bClassification1 = bClassificationList.get(i);
                    bClassification1.setSyncStatus(1L);
                    ibClassificationService.updateBClassification(bClassification1);
                }
            }
        } else {

        }
        AjaxResult ajax = AjaxResult.success();
        return ajax;
    }

    @GetMapping("/jobsnew")
    public AjaxResult getJobs(){
        //2.同步工作
        BThing bThing = new BThing();
        bThing.setSyncStatus(1L);
        List<BThing> bThingList = ibThingService.selectBThingList(bThing);
        if (bThingList != null && bThingList.size() > 0) {
            for (int j = 0; j < bThingList.size(); j++) {
                String title = bThingList.get(j).getTitle();
                Long classificationId = bThingList.get(j).getClassificationId();
                BClassification bClassification1 =
                        ibClassificationService.selectBClassificationById(classificationId + "");
                if (bClassification1 == null) {
                    throw new ServiceException("異常");
                }
                PPosition pPosition = new PPosition();
                pPosition.setName(bClassification1.getTitle());
                pPosition.setpId(null);
                List<PPosition> positionList =
                        positionService.selectPPositionList(pPosition);
                if (positionList == null || positionList.size() <= 0) {
                    throw new ServiceException("異常");
                }
                // 添加到分类中
                PPosition pPosition3 = new PPosition();
                pPosition3.setName(title);
                String id3 = new UuidUtils().getShortUuid();
                pPosition3.setId(id3);
                pPosition3.setEnableStatus(0);
                pPosition3.setpId(positionList.get(positionList.size() - 1).getId());
                positionService.insertPPosition(pPosition3);

                PPosition pPosition4 = new PPosition();
                pPosition4.setName(title);
                String id4 = new UuidUtils().getShortUuid();
                pPosition4.setId(id4);
                pPosition4.setEnableStatus(0);
                pPosition4.setpId(id3);
                positionService.insertPPosition(pPosition4);

                Job job = new Job();
                String id = new UuidUtils().getShortUuid();
                job.setId(id);
                job.setCompanyId("4Lkoah56");
                job.setPostId(id4);
                job.setEnableStatus(0);
                //工作经验
                if (!StringUtils.isBlank(bThingList.get(j).getWorkExpe())) {
                    if (bThingList.get(j).getWorkExpe().equals("不限")) {
                        job.setExpRequire(0L);
                    } else if (bThingList.get(j).getWorkExpe().equals("1年以内")) {
                        job.setExpRequire(1L);
                    } else if (bThingList.get(j).getWorkExpe().equals("1-3年")) {
                        job.setExpRequire(2L);
                    } else if (bThingList.get(j).getWorkExpe().equals("3-5年")) {
                        job.setExpRequire(3L);
                    } else if (bThingList.get(j).getWorkExpe().equals("5-10年")) {
                        job.setExpRequire(4L);
                    } else if (bThingList.get(j).getWorkExpe().equals("10年以上")) {
                        job.setExpRequire(5L);
                    }
                }
                // 学历
                if (!StringUtils.isBlank(bThingList.get(j).getEducation())) {
                    if (bThingList.get(j).getEducation().equals("不限")) {
                        job.setMinEducation(7L);
                    } else if (bThingList.get(j).getEducation().equals("高中以下")){
                        job.setMinEducation(0L);
                    } else if (bThingList.get(j).getEducation().equals("高中")){
                        job.setMinEducation(1L);
                    } else if (bThingList.get(j).getEducation().equals("中专/技校")){
                        job.setMinEducation(2L);
                    } else if (bThingList.get(j).getEducation().equals("大专")){
                        job.setMinEducation(3L);
                    } else if (bThingList.get(j).getEducation().equals("本科")){
                        job.setMinEducation(4L);
                    } else if (bThingList.get(j).getEducation().equals("硕士")){
                        job.setMinEducation(5L);
                    } else if (bThingList.get(j).getEducation().equals("博士")){
                        job.setMinEducation(6L);
                    }
                }
                //薪资范围
                if (!StringUtils.isBlank(bThingList.get(j).getSalary())) {
                    if (bThingList.get(j).getSalary().equals("面議")) {
                        job.setSalary(0L);
                    } else if (bThingList.get(j).getSalary().equals("1-3K")) {
                        job.setSalary(1L);
                    } else if (bThingList.get(j).getSalary().equals("3-5K")) {
                        job.setSalary(2L);
                    } else if (bThingList.get(j).getSalary().equals("5-8K")) {
                        job.setSalary(3L);
                    } else if (bThingList.get(j).getSalary().equals("8-12K")) {
                        job.setSalary(4L);
                    } else if (bThingList.get(j).getSalary().equals("12-15K")) {
                        job.setSalary(5L);
                    } else if (bThingList.get(j).getSalary().equals("15-20K")) {
                        job.setSalary(6L);
                    } else if (bThingList.get(j).getSalary().equals("20K以上")) {
                        job.setSalary(7L);
                    }
                }
                if (bThingList.get(j).getArea().contains("香港")) {
                    job.setJobType(1L);
                } else {
                    job.setJobType(0L);
                }

                job.setSkill(bThingList.get(j).getDescription());
                jobService.insertJob(job);
                BThing bThing1 = bThingList.get(j);
                bThing1.setSyncStatus(0L);
                ibThingService.updateBThing(bThing1);
            }
        }
        AjaxResult ajax = AjaxResult.success();
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
